﻿@using Mapster;
@using OnWork.DataCenter.BLL.Standard;
<ValidateForm Model="@Config"
              OnValidSubmit="@OnValidSubmit">
    <div class="row">
        <div class="col-12">
            <Table TItem="VariableDto" UseInjectDataService="false" IsPagination="true" DoubleClickToEdit="true"
                   IsStriped="true" IsBordered="true" ShowSkeleton="true" IsMultipleSelect="true" ShowEmpty="false" ShowLoading="true" ShowColumnList="true"
                   ShowToolbar="true" ShowExtendButtons="true"
                   ShowSearch="true" ShowAdvancedSearch="false"
                   AutoGenerateColumns="false" EditMode="EditMode.InCell"
                   OnSaveAsync="@OnSaveAsync" OnDeleteAsync="@OnDeleteAsync"
                   ShowToastAfterSaveOrDeleteModel="false" Items="@Config.Variables">
                <TableColumns>
                    <TableColumn Text="字段名" @bind-Field="@context.VariableName" />
                    <TableColumn Text="查询条件" @bind-Field="@context.VariableSelectToken" />
                    <TableColumn Text="说明" @bind-Field="@context.Remark" />
                </TableColumns>
            </Table>
        </div>
    </div>
    <div class="form-footer mt-3">
        <DialogCloseButton />
        <Button Color="Color.Primary" ButtonType="ButtonType.Submit" Icon="fa-solid fa-floppy-disk" Text="保存" />
    </div>
</ValidateForm>

@code {
    /// <summary>
    /// Json转DataTable配置
    /// </summary>
    private OnWork.DataCenter.BLL.Standard.JsonToVariablesConfig Config = new BLL.Standard.JsonToVariablesConfig() { Variables = new List<BLL.Standard.VariableDto>() };

    [Parameter]
    public Action<JsonToVariablesConfig>? OnSave { get; set; }

    [CascadingParameter(Name = "BodyContext")]
    private object? Context { get; set; }

    [CascadingParameter]
    private Modal? Dialog { get; set; }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();

        if (Context is OnWork.DataCenter.BLL.Standard.JsonToVariablesConfig)
        {
            var config = Context as OnWork.DataCenter.BLL.Standard.JsonToVariablesConfig;

            if (config is not null)
            {
                Config = config;
            }

        }

    }

    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="items"></param>
    /// <returns></returns>
    private async Task<bool> OnDeleteAsync(IEnumerable<VariableDto> items)
    {
        Config.Variables.RemoveAll(x => items.Contains(x));
        return await Task.FromResult(true); ;
    }

    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="item"></param>
    /// <param name="changedType"></param>
    /// <returns></returns>
    private async Task<bool> OnSaveAsync(VariableDto item, ItemChangedType changedType)
    {
        if (changedType == ItemChangedType.Update) // 更新
        {
            var entry = Config.Variables.Where(x => x.id == item.id).FirstOrDefault();
            item.Adapt(entry);
            return await Task.FromResult(true); 
        }
        else //新增
        {
            Config.Variables.Add(item);
            return await Task.FromResult(true); 
        }
    }


    private async Task OnValidSubmit(EditContext item)
    {
        // do someting like save model into db

        if (OnSave is not null)
        {
            OnSave(Config);
        }

        // 关闭弹窗
        // close Dialog
        if (Dialog!=null)
        {
            await Dialog.Close();
        }
      
    }
}
